<?php

class Factura {
    public $id_factura;         //id-ul facturii in tabel
    public $id_comanda;
    public $moneda;
    public $curs_1;         
    public $curs_2;            
    public $text_factura;
    public $serie_factura;      // de luat din tabelul comanda
    public $nume_delegat;
    public $mijloc_transport;    
    public $cnp_delegat;    
    public $serie_ci_delegat;
    public $tva_inclus;
    public $cota_tva;
    public $data;
    public $created_by;

    function __construct($id_comanda, $moneda, $curs_1, $curs_2, $text_factura, $nume_delegat, $mijloc_transport
                            , $cnp_delegat, $serie_ci_delegat, $tva_inclus, $cota_tva, $created_by) {
        $this->id_factura = CT_NOT_SAVED_YET;
        $this->id_comanda = $id_comanda;
        $this->moneda = $moneda;
        $this->curs_1 = $curs_1;
        $this->curs_2 = $curs_2;
        $this->text_factura = $text_factura;
        $this->serie_factura = '';
        $this->nume_delegat = $nume_delegat;
        $this->mijloc_transport = $mijloc_transport;
        $this->cnp_delegat = $cnp_delegat;
        $this->serie_ci_delegat = $serie_ci_delegat;
        $this->tva_inclus = $tva_inclus;
        $this->cota_tva = $cota_tva;
        $this->created_by = $created_by;
    }
    
    public function queryInsert($db) {
        $query = $db->prepare("INSERT INTO `factura`  ( `id_comanda` ,         `curs_1` , 
                                                        `curs_2` ,             `text_factura`,
                                                        `nume_delegat` ,        `mijloc_transport`,
                                                        `cnp_delegat`,          `serie_ci_delegat`,
                                                        `tva_inclus`,           `cota_tva`,
                                                        `created_by`,           `moneda`,
                                                        `data`  
                                                       ) 
                                        VALUES      (    :id_comanda,          :curs_1, 
                                                        :curs_2,               :text_factura,
                                                        :nume_delegat,         :mijloc_transport,
                                                        :cnp_delegat,           :serie_ci_delegat,
                                                        :tva_inclus,            :cota_tva,
                                                        :created_by,            :moneda,
                                                        NOW()
                                                      );");
        
        $query->bindParam(':id_comanda',     ($this->id_comanda));
        $query->bindParam(':moneda',         ($this->moneda));
        $query->bindParam(':curs_1',         ($this->curs_1));
        $query->bindParam(':curs_2',         ($this->curs_2));
        $query->bindParam(':text_factura',     ($this->text_factura));
        $query->bindParam(':nume_delegat', ($this->nume_delegat));
        $query->bindParam(':mijloc_transport',     ($this->mijloc_transport));
        $query->bindParam(':cnp_delegat',     ($this->cnp_delegat));
        $query->bindParam(':serie_ci_delegat',     ($this->serie_ci_delegat));
        $query->bindParam(':tva_inclus',     ($this->tva_inclus));
        $query->bindParam(':cota_tva',     ($this->cota_tva));
        $query->bindParam(':created_by',     ($this->created_by));
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
              $query->closeCursor();
            throw new QueryInsertFacturaException("Failed saving Factura to the database" . $errInfo);        
        }
              
          $this->id_factura = (int) $db->lastInsertId(); 
          $query->closeCursor();
        return $this->id_factura;         //return the factura's id
    }
    
    public function queryUpdate($db) {                                      
        $this->version++;
        
        $query = $db->prepare("UPDATE `factura`  SET     
                                `id_comanda` = :id_comanda ,            `curs_1` =  :curs_1, 
                                `curs_2` = :curs_2,                     `text_factura` = :text_factura,
                                `nume_delegat` = :nume_delegat,         `mijloc_transport` = :mijloc_transport,
                                `cnp_delegat` = :cnp_delegat,           `serie_ci_delegat` = :serie_ci_delegat,
                                `tva_inclus` = :tva_inclus,             `cota_tva` = :cota_tva,
                                `created_by` = :created_by,             `moneda` = :moneda
                                WHERE id = :id_factura
                                AND version < :version");
        
        $query->bindParam(':id_comanda',     ($this->id_comanda));
        $query->bindParam(':moneda',         ($this->moneda));
        $query->bindParam(':curs_1',         ($this->curs_1));
        $query->bindParam(':curs_2',         ($this->curs_2));
        $query->bindParam(':text_factura',     ($this->text_factura));
        $query->bindParam(':nume_delegat', ($this->nume_delegat));
        $query->bindParam(':mijloc_transport',     ($this->mijloc_transport));
        $query->bindParam(':cnp_delegat',     ($this->cnp_delegat));        
        $query->bindParam(':serie_ci_delegat',     ($this->serie_ci_delegat));        
        $query->bindParam(':tva_inclus',     ($this->tva_inclus));        
        $query->bindParam(':cota_tva',     ($this->cota_tva));        
        $query->bindParam(':created_by',     ($this->created_by));        
        $query->bindParam(':id_factura',     ($this->id_factura));        
        $query->bindParam(':version',     ($this->version));        
                               
        if ($query->execute() === FALSE) {            
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryUpdateFacturaException("Failed updating Factura with id: " . $this->id_factura . $errInfo);
        }
            
        if ($query->rowCount() == 0) {
               $query->closeCursor();
            throw new QueryWrongVersionException("Failed updating Factura. Reason: wrong version!");
        }
           
        $query->closeCursor();          
        return CT_RESULT_OK;
    }
    
      public static function queryLoadFacturaByCommandId($db, $id_comanda) {
        $query = $db->prepare("SELECT f.*, c.nr_factura_sol
                                FROM factura f, comanda c
                                WHERE id_comanda = :id_comanda
                                AND   c.id = :id_comanda");
        $query->bindParam(':id_comanda',     $id_comanda);                                
        
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryLoadFacturaException("Faild loading factura with id_comanda: " . $id_comanda . $errInfo);
        }
        
        $data = $query->fetch(PDO::FETCH_ASSOC);
                   
        if (!isset($data['id'])) {
            $query->closeCursor();
            return CT_NO_FACTURA_FOUND;
        }
                            
        $factura = new Factura($data['id_comanda'], $data['moneda'], $data['curs_1']
                            , $data['curs_2'], $data['text_factura'], $data['nume_delegat']
                            , $data['mijloc_transport'], $data['cnp_delegat'], $data['serie_ci_delegat']
                            , $data['tva_inclus'], $data['cota_tva'], $data['created_by']);
        $factura->id_factura = (int) $data['id'];
        $factura->data = $data['data'];
        $factura->version = $data['version'];
        $query->closeCursor();
        return $factura;
    }    
}

?>